/**
 * Copyright (c) 2011 Alexander De Leon Battista
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * 
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package es.upm.oeg.presto.inject;

import java.net.URI;
import java.util.Arrays;

import net.xeoh.plugins.base.Plugin;
import net.xeoh.plugins.base.PluginManager;
import net.xeoh.plugins.base.options.AddPluginsFromOption;
import net.xeoh.plugins.base.options.GetPluginOption;
import net.xeoh.plugins.base.options.getplugin.OptionPluginSelector;
import net.xeoh.plugins.base.options.getplugin.PluginSelector;

import com.google.inject.MembersInjector;

/**
 * @author Alexander De Leon
 */
public class PluginManagerGuiceWrapper implements PluginManager {

	private final PluginManager delegate;
	private final MembersInjector<Plugin> injector;

	public PluginManagerGuiceWrapper(PluginManager delegate, MembersInjector<Plugin> injector) {
		super();
		this.delegate = delegate;
		this.injector = injector;
	}

	@Override
	public void addPluginsFrom(URI arg0, AddPluginsFromOption... arg1) {
		delegate.addPluginsFrom(arg0, arg1);

	}

	@Override
	public <P extends Plugin> P getPlugin(Class<P> arg0, GetPluginOption... arg1) {
		GetPluginOption[] options = Arrays.copyOf(arg1, arg1.length + 1);
		options[arg1.length] = new OptionPluginSelector<Plugin>(new PluginSelector<Plugin>() {

			@Override
			public boolean selectPlugin(Plugin p) {
				injector.injectMembers(p);
				return true;
			}

		});
		return delegate.getPlugin(arg0, options);
	}

	@Override
	public void shutdown() {
		delegate.shutdown();

	}

}
